package com.apes.framework.plugin.fsm.schedule;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.text.SimpleDateFormat;

/**
 * 功能：排班明细
 *
 * @author xul
 * @create 2021-01-07 11:37
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = WorkScheduleItem.CONST_TABLE_NAME)
public class WorkScheduleItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "fsm_work_schedule_item";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "排班明细")
    @Comment("编码")
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "work_schedule_id", referencedColumnName = "id", nullable = false)
    @Comment("排班")
    private WorkSchedule workSchedule;

    @Column(name = "start_", length = 10)
    @Comment("开始（格式：HH:mm）")
    private String start;

    @Column(name = "end_", length = 10)
    @Comment("结束（格式：HH:mm）")
    private String end;

    @SneakyThrows
    public long caleStartTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.parse(this.start).getTime();
    }
}
